//
//  ViewController.m
//  本地通知
//
//  Created by Justsoso on 16/4/1.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)addbtnPressed:(id)sender
{
    [self addLocalNotification];
}
- (IBAction)cancelNotiBtnPressed:(id)sender
{
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *notis = app.scheduledLocalNotifications;
    
    for (UILocalNotification *n in notis)
    {
        NSDictionary *dict = n.userInfo;
        if ([dict[@"notiId"] isEqualToString:@"1"])
        {
            [app cancelLocalNotification:n];
        }
    }
}

- (void)addLocalNotification
{
    UILocalNotification *n = [[UILocalNotification alloc] init];
    // 触发的时间
    n.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    
    // 是否重复触发
    n.repeatInterval = kCFCalendarUnitDay;// 每天这个时候
    
    // 显示信息
    n.alertBody = @"时间到了，洗洗睡吧";
    n.alertAction = @"时间到了，洗洗睡吧";
    
    // 声音
    n.soundName = @"myMusic.caf";
    
    //角标
    n.applicationIconBadgeNumber = [UIApplication sharedApplication].scheduledLocalNotifications.count+1;
    
    // 附带的信息
    n.userInfo = @{@"notiId":@"1"};
    
    // 计划这个通知
    [[UIApplication sharedApplication] scheduleLocalNotification:n];
}
@end
